import axios from 'axios'
import { Message } from 'element-ui'

// 创建axios实例
const service = axios.create({
    baseURL: 'http://localhost:8081/flower-store', // api 的默认 base_url
    timeout: 20000 //请求超时时间
})

// request拦截器
service.interceptors.request.use(
    config => {
        return config
    },
    error => {
        // Do something with request error
        Promise.reject(error)
    }
)

// response 拦截器
service.interceptors.response.use(
    response => {
        const res = response.data;
        if(res.code === '001'){
            return response.data;
        }else{
            Message({
                message: res.msg,
                type: 'error',
                duration: 5 * 1000
            })
            return Promise.reject('error')
        }
    },
    error => {
        Message({
            message: error.message,
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject('error')
    }
)

export default service